package com.example.androidtest3

import android.R.attr.data
import android.app.ComponentCaller
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.EditText
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import org.w3c.dom.Text

const val FIRST_MSG="FIRST_MSG"
const val SECOND_MSG="SECOND_MSG"
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        val first = findViewById<Button>(R.id.first)
        val editText = findViewById<EditText>(R.id.editText)
        val second = findViewById<Button>(R.id.second)
        //第一个二级界面实现
        first.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java);//获取意图，建立第一个主页面和第二个页面的联系
            intent.putExtra(FIRST_MSG, editText.text.toString())//参数是设置的常量键和我获取输入文本的内容
            startActivity(intent)//启动意图，也就是启动这个页面跳转
        }

        //第二个二级界面实现
        //startActivityForResult是启动另一个界面的跳转并传回参数
        second.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java);
            startActivityForResult(intent, 0)
        }

        //点击按钮打开相机
        val button3 = findViewById<Button>(R.id.button3)
        button3.setOnClickListener {
            val intentButton= Intent(this, FourthActivity::class.java)
            startActivity(intentButton)
        }

        //点击打开计时器页面
        val button=findViewById<Button>(R.id.button4)
        button.setOnClickListener {
            val Fintent=Intent(this, fifthActivity::class.java)
            startActivity(Fintent);
        }
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    val request_code = 1
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 1 && requestCode == 0) {//去判断请求码和结果码
            val textView = findViewById<TextView>(R.id.textView)
            textView.text = data?.getStringExtra(SECOND_MSG)//获取另一个页面传回来的参数并把它写入文本中
        }

    }
    //调用相机函数

}